class Solution:
    def maxProductPath(self, grid: list) -> int:
        dp = [[[1, 1] for _ in range(len(grid[i]))] for i in range(len(grid))]
        for i in range(len(grid)):
            for j in range(len(grid[i])):
                if i - 1 < 0 and j - 1 < 0:
                    dp[i][j][0] = grid[i][j]
                    dp[i][j][1] = grid[i][j]
                elif i - 1 < 0:
                    dp[i][j][0] = max(dp[i][j - 1][0] * grid[i][j],
                                      dp[i][j - 1][1] * grid[i][j])
                    dp[i][j][1] = min(dp[i][j - 1][0] * grid[i][j],
                                      dp[i][j - 1][1] * grid[i][j])
                elif j - 1 < 0:
                    dp[i][j][0] = max(dp[i - 1][j][0] * grid[i][j],
                                      dp[i - 1][j][1] * grid[i][j])
                    dp[i][j][1] = min(dp[i - 1][j][0] * grid[i][j],
                                      dp[i - 1][j][1] * grid[i][j])
                else:
                    dp[i][j][0] = max(dp[i][j - 1][0] * grid[i][j],
                                      dp[i - 1][j][0] * grid[i][j],
                                      dp[i][j - 1][1] * grid[i][j],
                                      dp[i - 1][j][1] * grid[i][j])
                    dp[i][j][1] = min(dp[i][j - 1][1] * grid[i][j],
                                      dp[i - 1][j][1] * grid[i][j],
                                      dp[i][j - 1][0] * grid[i][j],
                                      dp[i - 1][j][0] * grid[i][j])

        return dp[-1][-1][0] % (10**9 + 7) if dp[-1][-1][0] >= 0 else -1
